package vip.xiaonuo.client.modular.wxpay.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 微信支付V3配置类
 * 
 * @author Vincent
 */
@Slf4j
@Configuration
public class WxPayV3Configuration {
    
    @Value("${wx.pay.appId}")
    private String appId;
    
    @Value("${wx.pay.mchId}")
    private String mchId;
    
    @Value("${wx.pay.keyPath}")
    private String keyPath;
    
    @Value("${wx.pay.apiv3-key}")
    private String apiV3Key;
    
    @Value("${wx.pay.cert-serial-no}")
    private String certSerialNo;
    
    @Value("${wx.pay.private-key-path}")
    private String privateKeyPath;
    
    @Value("${wx.pay.private-cert-path}")
    private String privateCertPath;
    
    @Value("${wx.pay.notifyUrl}")
    private String notifyUrl;
    
    @Value("${wx.pay.sandbox:false}")
    private boolean sandbox;
    
    /**
     * 微信支付V3服务
     */
    @Bean("wxPayV3Service")
    public WxPayService wxPayService() {
        log.info("初始化微信支付V3服务，商户号：{}, 应用ID：{}", mchId, appId);
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(appId);
        payConfig.setMchId(mchId);
        
        // 设置API v3密钥
        payConfig.setApiV3Key(apiV3Key);
        
        // 设置证书相关
        payConfig.setKeyPath(keyPath);
        payConfig.setCertSerialNo(certSerialNo);
        payConfig.setPrivateKeyPath(privateKeyPath);
        payConfig.setPrivateCertPath(privateCertPath);
        
        // 设置其他配置
        payConfig.setNotifyUrl(notifyUrl);
        payConfig.setUseSandboxEnv(sandbox);
        
        // 创建服务
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        
        return wxPayService;
    }
} 